package com.exam.online.system.util;

import com.exam.online.system.constant.StatusCode;
import com.fasterxml.jackson.annotation.JsonInclude;

/**
 * @author xupei
 * @date 2019/1/25
 */
public class ResponseModel<T> {

    private String code;
    private String msg;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    private T data;

    public static <T> ResponseModel<T> buildFromStateCode(StatusCode statusCode) {
        return buildFromStateCode(statusCode, null);
    }

    public static <T> ResponseModel<T> buildFromStateCode(StatusCode statusCode, T data) {
        ResponseModel<T> responseModel = new ResponseModel<>();
        responseModel.setCode(statusCode.getCode());
        responseModel.setMsg(statusCode.getMessage());
        responseModel.setData(data);
        return responseModel;
    }

    public static <T> ResponseModel<T> build(String code, String msg) {
        return build(code, msg, null);
    }

    public static <T> ResponseModel<T> build(String code, String msg, T data) {
        ResponseModel<T> responseModel = new ResponseModel<>();
        responseModel.setCode(code);
        responseModel.setMsg(msg);
        responseModel.setData(data);
        return responseModel;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
